package com.baobao.web;

import com.baobao.dao.AskleaveDAO;

import com.baobao.domain.Askleave;

import com.baobao.service.AskleaveService;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Askleave entities
 * 
 */

@Controller("AskleaveController")
public class AskleaveController {

	/**
	 * DAO injected by Spring that manages Askleave entities
	 * 
	 */
	@Autowired
	private AskleaveDAO askleaveDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Askleave entities
	 * 
	 */
	@Autowired
	private AskleaveService askleaveService;

	/**
	 * Edit an existing Askleave entity
	 * 
	 */
	@RequestMapping("/editAskleave")
	public ModelAndView editAskleave(@RequestParam String parentPhoneKey, @RequestParam String studentIdKey, @RequestParam Calendar applyTimeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("askleave", askleaveDAO.findAskleaveByPrimaryKey(parentPhoneKey, studentIdKey, applyTimeKey));
		mav.setViewName("askleave/editAskleave.jsp");

		return mav;
	}

	/**
	 * Delete an existing Askleave entity
	 * 
	 */
	@RequestMapping("/deleteAskleave")
	public String deleteAskleave(@RequestParam String parentPhoneKey, @RequestParam String studentIdKey, @RequestParam Calendar applyTimeKey) {
		Askleave askleave = askleaveDAO.findAskleaveByPrimaryKey(parentPhoneKey, studentIdKey, applyTimeKey);
		askleaveService.deleteAskleave(askleave);
		return "forward:/indexAskleave";
	}

	/**
	 */
	@RequestMapping("/askleaveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Save an existing Askleave entity
	 * 
	 */
	@RequestMapping("/saveAskleave")
	public String saveAskleave(@ModelAttribute Askleave askleave) {
		askleaveService.saveAskleave(askleave);
		return "forward:/indexAskleave";
	}

	/**
	 * Select an existing Askleave entity
	 * 
	 */
	@RequestMapping("/selectAskleave")
	public ModelAndView selectAskleave(@RequestParam String parentPhoneKey, @RequestParam String studentIdKey, @RequestParam Calendar applyTimeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("askleave", askleaveDAO.findAskleaveByPrimaryKey(parentPhoneKey, studentIdKey, applyTimeKey));
		mav.setViewName("askleave/viewAskleave.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Askleave entities
	 * 
	 */
	public String indexAskleave() {
		return "redirect:/indexAskleave";
	}

	/**
	 * Select the Askleave entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteAskleave")
	public ModelAndView confirmDeleteAskleave(@RequestParam String parentPhoneKey, @RequestParam String studentIdKey, @RequestParam Calendar applyTimeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("askleave", askleaveDAO.findAskleaveByPrimaryKey(parentPhoneKey, studentIdKey, applyTimeKey));
		mav.setViewName("askleave/deleteAskleave.jsp");

		return mav;
	}

	/**
	 * Show all Askleave entities
	 * 
	 */
	@RequestMapping("/indexAskleave")
	public ModelAndView listAskleaves() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("askleaves", askleaveService.loadAskleaves());

		mav.setViewName("askleave/listAskleaves.jsp");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Create a new Askleave entity
	 * 
	 */
	@RequestMapping("/newAskleave")
	public ModelAndView newAskleave() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("askleave", new Askleave());
		mav.addObject("newFlag", true);
		mav.setViewName("askleave/editAskleave.jsp");

		return mav;
	}
}